﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MESPlotV1
{
    public partial class FHCMESExtendDemo : Form
    {
        private bool isInit = true;
        public FHCMESExtendDemo()
        {
            Program.SQLSerConfig = new SQLSerConfigReg("HCMesExtend3");
            isInit = true;
            InitializeComponent();
            try
            {
                textBoxServerAddress.Text = Program.SQLSerConfig.Address;
                textBoxUserName.Text = Program.SQLSerConfig.DBUser;
                textBoxPassword.Text = Program.SQLSerConfig.DBPass;
                textBoxDBName.Text = Program.SQLSerConfig.DBName;
            }
            finally
            {
                isInit = false;
            }
        }

        private void buttonSaveBack_Click(object sender, EventArgs e)
        {
            IPAddress tNewIP = null;
            if (!isInit)
            {
                if (!IPAddress.TryParse(textBoxServerAddress.Text, out tNewIP))                
                    throw new Exception("请输入正确的IP地址");

                if (string.IsNullOrEmpty(textBoxUserName.Text) || string.IsNullOrEmpty(textBoxPassword.Text)
                    || (textBoxUserName.Text.Length < 2) || (textBoxPassword.Text.Length < 2))
                    throw new Exception("请输入用户名密码");
                if (SQLSerHelper.checkDBConnect(textBoxServerAddress.Text,
                    textBoxUserName.Text,
                    textBoxPassword.Text,
                    textBoxDBName.Text))
                {
                    Program.SQLSerConfig.Address = textBoxServerAddress.Text;
                    Program.SQLSerConfig.DBUser = textBoxUserName.Text;
                    Program.SQLSerConfig.DBPass = textBoxPassword.Text;
                    Program.SQLSerConfig.DBName = textBoxDBName.Text;
                    groupBoxDBConfig.Enabled = false;
                    //MessageBox.Show("数据库连接成功！");
                    panelMain.Visible = true;
                }
                else
                    throw new Exception("数据库连接失败！");
            }
        }

        private void button不合格通知更改单套打_Click(object sender, EventArgs e)
        {
            F不合格通知处理单套打 tPlot = new F不合格通知处理单套打(Program.SQLSerConfig);
            tPlot.ShowDialog();
        }
    }
}
